package com.ygk.bluepath.member.View;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.ygk.bluepath.R;
import com.ygk.bluepath.core.BFragment;
import com.ygk.bluepath.member.Presenter.RegisterPresenterFragmentTwo;

import utils.ImageUtil;
import utils.Inf;
import utils.LogUtils;

/**
 * Created by Handreace on 16-7-13.
 */
public class RegisterFragmentTwo extends BFragment implements View.OnClickListener, RegisterFragmentTwoView {
    private View mRootView;

    @ViewInject(R.id.et_realname)
    private EditText et_realname;
    @ViewInject(R.id.et_identification)
    private EditText et_identification;
    @ViewInject(R.id.et_password)
    private EditText et_password;
    @ViewInject(R.id.et_repeat_password)
    private EditText et_repeat_password;

    @ViewInject(R.id.iv_identification_front)
    private ImageView iv_identification_front;
    @ViewInject(R.id.iv_identification_back)
    private ImageView iv_identification_back;
    @ViewInject(R.id.iv_identification_and_face)
    private ImageView iv_identification_and_face;

    private RegisterPresenterFragmentTwo registerPresenterFragmentTwo;

    private boolean isChooseFront=false;//是否选择了身份证正面照
    private boolean isChooseBack=false;//是否选择了身份证背面照
    private boolean isChooseFace=false;//是否选择了身份证半身照

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.fragment_register_step2, container, false);
        ViewUtils.inject(this,mRootView);
        registerPresenterFragmentTwo = new RegisterPresenterFragmentTwo(this, this);
        initView();
        return mRootView;
    }

    private void initView() {

        // ((ImageView) mRootView.findViewById(R.id.iv_step)).setImageDrawable(getResources().getDrawable(R.drawable.ic_launcher));

        mRootView.findViewById(R.id.tv_next).setOnClickListener(this);
        mRootView.findViewById(R.id.ll_identification_front).setOnClickListener(this);
        mRootView.findViewById(R.id.ll_identification_back).setOnClickListener(this);
        mRootView.findViewById(R.id.ll_identification_and_face).setOnClickListener(this);

    }

    private int takePicType=0;//选择图片的类型:默认0 身份证正面照;1 身份证背面照 ; 2 身份证半身照

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_identification_front://身份证正面照选择
                takePicType=0;
                registerPresenterFragmentTwo.choosePic();

                break;
            case R.id.ll_identification_back://身份证背面照选择
                takePicType=1;
                registerPresenterFragmentTwo.choosePic();

                break;
            case R.id.ll_identification_and_face://身份证半身照选择
                takePicType=2;
                registerPresenterFragmentTwo.choosePic();

                break;

            case R.id.tv_next://下一步

                registerPresenterFragmentTwo.next(et_realname.getText().toString(),et_identification.getText().toString(),et_password.getText().toString(),et_repeat_password.getText().toString(),isChooseFront,isChooseBack,isChooseFace);

                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case Inf.TAKE_PICTURE_CAMERA: // --------------------camera

                if (resultCode == getActivity().RESULT_OK) {
                    tt("图片处理中，请稍后……");
                    //addInserPicFirst(path, false);
                    setView(Inf.cameraPath);
                }else {
                    tt("照片获取失败");
                }

                break;

            case Inf.TAKE_PICTURE_ALBUMS:// ---------------------------albums
                if (data != null) {
                    if (resultCode == getActivity().RESULT_OK) {
                        tt("图片处理中，请稍后……");
                        setView(ImageUtil.getRealFilePath(getActivity(),
                                        data.getData()));
                       /* addInserPicFirst(
                                UiOperateUtil.getRealFilePath(ACHRelease.this,
                                        data.getData()), false);*/
                    } else {
                        tt("照片获取失败");
                    }
                }

                break;
        }
    }


    private void setView(final String path){
        LogUtils.e("iv_identification_front"+iv_identification_front==null);
        switch (takePicType){//选择图片的类型:默认0 身份证正面照;1 身份证背面照 ; 2 身份证半身照
            case 0:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        ImageUtil.savePicfileWithMaxSize(path,"identification_front.jpg",getPhotos_cache(),200);
                            getActivity().runOnUiThread(new Runnable() {
                                public void run() {
                                    disPlayImage("file:///"+path, iv_identification_front);
                                    isChooseFront=true;
                                    ((RegisterActivity) getActivity()).setIdentificationFrontUrl(getPhotos_cache()+"identification_front.jpg");
                                }
                            });
                    }
                }).start();
                break;
            case 1:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        ImageUtil.savePicfileWithMaxSize(path,"identification_back.jpg",getPhotos_cache(),200);
                        getActivity().runOnUiThread(new Runnable() {
                            public void run() {
                                disPlayImage("file:///"+path, iv_identification_back);
                                isChooseBack=true;
                                ((RegisterActivity) getActivity()).setIdentificationBackUrl(getPhotos_cache()+"identification_back.jpg");
                            }
                        });
                    }
                }).start();
                break;
            case 2:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        ImageUtil.savePicfileWithMaxSize(path,"identification_face.jpg",getPhotos_cache(),200);
                        getActivity().runOnUiThread(new Runnable() {
                            public void run() {
                                disPlayImage("file:///"+path, iv_identification_and_face);
                                isChooseFace=true;
                                ((RegisterActivity) getActivity()).setIdentificationFaceUrl(getPhotos_cache()+"identification_face.jpg");
                            }
                        });
                    }
                }).start();
                break;
        }
    }

}
